package com.lh.blogConsumer.service.Impl;

import com.lh.blogCommon.util.PageUtils;
import com.lh.blogCommon.util.Query;
import com.lh.blogConsumer.feign.OssFeignService;
import com.lh.blogConsumer.mapper.FeaturedIrstMapper;
import com.lh.blogConsumer.entity.FeaturedIrstEntity;
import com.lh.blogConsumer.service.FeaturedIrstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;


@Service
public class FeaturedIrstServiceImpl extends ServiceImpl<FeaturedIrstMapper, FeaturedIrstEntity> implements FeaturedIrstService {

    @Autowired
    OssFeignService ossFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<FeaturedIrstEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        System.out.println(params);
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("featured_id",key).or()
                    .eq("featured_name",key).or()
                    .eq("creation_time", key).or()
                    .eq("modification_time", key);

        }
        queryWrapper.orderByDesc("featured_id");

        IPage<FeaturedIrstEntity> page = this.page(
                new Query<FeaturedIrstEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void removeAdImageByIds(String[] featuredIds) {
        List<String> imgs = baseMapper.selectBatchIds(Arrays.asList(featuredIds)).stream().map((item) -> {
            return item.getFeaturedImage();
        }).collect(Collectors.toList());
        ossFeignService.removeList(imgs);
    }

}